package com.xy.biz.fd.domain;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 文档权限
 */
public class DocumentPermissions {

    /**
     * 可见
     */
    public final static int VISIBLE = 1;
    /**
     * 预览
     */
    public final static int PREVIEW = 2;
    /**
     * 打印
     */
    public final static int PRINT = 4;
    /**
     * 下载
     */
    public final static int DOWNLOAD = 8;
    /**
     * 重命名
     */
    public final static int RENAME = 16;
    /**
     * 收藏
     */
    public final static int FAVORITE = 32;
    /**
     * 移动
     */
    public final static int MOVE = 64;
    /**
     * 复制
     */
    public final static int COPY = 128;
    /**
     * 编辑
     */
    public final static int EDIT = 256;
    /**
     * 新增
     */
    public final static int NEW = 512;
    /**
     * 分享
     */
    public final static int SHARE = 1024;
    /**
     * 关联
     */
    public final static int ASSOCIATE = 2048;
    /**
     * 删除
     */
    public final static int DELETE = 4096;

//    private final static List<KeyValue<Integer, String>> permissionList;
//
//    static {
//        permissionList = new ArrayList<>();
//        permissionList.add(new KeyValue<>(VISIBLE, "可见"));
//        permissionList.add(new KeyValue<>(PREVIEW, "预览"));
//        permissionList.add(new KeyValue<>(PRINT, "打印"));
//        permissionList.add(new KeyValue<>(DOWNLOAD, "下载"));
//        permissionList.add(new KeyValue<>(RENAME, "重命名"));
//        permissionList.add(new KeyValue<>(FAVORITE, "收藏"));
//        permissionList.add(new KeyValue<>(MOVE, "移动"));
//        permissionList.add(new KeyValue<>(COPY, "复制"));
//        permissionList.add(new KeyValue<>(EDIT, "编辑"));
//        permissionList.add(new KeyValue<>(NEW, "新增"));
//        permissionList.add(new KeyValue<>(SHARE, "分享"));
//        permissionList.add(new KeyValue<>(ASSOCIATE, "关联"));
//        permissionList.add(new KeyValue<>(DELETE, "删除"));
//    }
//
//    public final static List<KeyValue<Integer, String>> getPermissionList() {
//        return permissionList;
//    }

    public final static boolean hasPermission(int permission, int item) {
        return (permission & item) == item;
    }

    public final static boolean hasVisible(int permission) {
        return (permission & VISIBLE) == VISIBLE;
    }

    public final static boolean hasPreview(int permission) {
        return (permission & PREVIEW) == PREVIEW;
    }

    public final static boolean hasPrint(int permission) {
        return (permission & PRINT) == PRINT;
    }

    public final static boolean hasDownload(int permission) {
        return (permission & DOWNLOAD) == DOWNLOAD;
    }

    public final static boolean hasRename(int permission) {
        return (permission & RENAME) == RENAME;
    }

    public final static boolean hasFavorite(int permission) {
        return (permission & FAVORITE) == FAVORITE;
    }

    public final static boolean hasMove(int permission) {
        return (permission & MOVE) == MOVE;
    }

    public final static boolean hasCopy(int permission) {
        return (permission & COPY) == COPY;
    }

    public final static boolean hasEdit(int permission) {
        return (permission & EDIT) == EDIT;
    }

    public final static boolean hasNew(int permission) {
        return (permission & NEW) == NEW;
    }

    public final static boolean hasShare(int permission) {
        return (permission & SHARE) == SHARE;
    }

    public final static boolean hasAssociate(int permission) {
        return (permission & ASSOCIATE) == ASSOCIATE;
    }

    public final static boolean hasDelete(int permission) {
        return (permission & DELETE) == DELETE;
    }

    public final static int addPermission(int... items) {
        if(items != null && items.length > 0) {
            Set<Integer> set = new HashSet<>();
            for(int item : items) {
                set.add(item);
            }
            int result = 0;
            for(Integer item : set) {
                result += item;
            }
            return result;
        }
        return 0;
    }

    public final static int addPermission(Collection<Integer> items) {
        if(items != null && items.size() > 0) {
            Set<Integer> set = new HashSet<>();
            for (int item : items) {
                set.add(item);
            }
            int result = 0;
            for (Integer item : set) {
                result += item;
            }
            return result;
        }
        return 0;
    }

    public static final Integer getAllPermissions() {
        Integer permissions = DocumentPermissions.VISIBLE +
                DocumentPermissions.PREVIEW +
                DocumentPermissions.PRINT +
                DocumentPermissions.DOWNLOAD +
                DocumentPermissions.RENAME +
                DocumentPermissions.FAVORITE +
                DocumentPermissions.MOVE +
                DocumentPermissions.COPY +
                DocumentPermissions.EDIT +
                DocumentPermissions.NEW +
                DocumentPermissions.SHARE +
                DocumentPermissions.ASSOCIATE +
                DocumentPermissions.DELETE;
        return permissions;
    }

    public static void main(String[] args) {
        System.out.println(hasPermission(3, PRINT));
        System.out.println(hasPermission(3, DOWNLOAD));
        System.out.println(hasPreview(3));
        System.out.println(hasPrint(3));
        System.out.println(hasDownload(3));
        System.out.println(hasDownload(addPermission(PREVIEW, DOWNLOAD)));
    }

}
